﻿using Inspire.Classes.Business;
using Inspire.Classes.Common;
using Inspire.Entities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows;

namespace Inspire.Windows
{
    /// <summary>
    /// Interaction logic for EditCustomFieldType.xaml
    /// </summary>
    public partial class AddCustomFieldType : Window
    {
        /// <summary>
        /// Get a reference to CustomFieldBusiness
        /// </summary>
        private CustomFieldBusiness _customFieldBusiness = CustomFieldBusiness.GetCustomFieldBusiness();

        /// <summary>
        /// The Custom Field being edited or created.
        /// </summary>
        private CustomFieldType _newCustomFieldType = new CustomFieldType();
        public CustomFieldType NewCustomFieldType
        {
            get
            {
                return this._newCustomFieldType;
            }
            set
            {
                this._newCustomFieldType = value;
            }
        }

        /// <summary>
        /// Default constructor.
        /// </summary>
        public AddCustomFieldType()
        {
            // Preset the new Field Type's values that come from a list to be the first option
           IEnumerator iEnum = Constants.CustomFieldCategoryLabelLookup.Keys.GetEnumerator();
            if (iEnum.MoveNext())
            {
                this._newCustomFieldType.Category = (string)iEnum.Current;
            }
            this._newCustomFieldType.Type = (string)Constants.CustomFieldTypeList[0];

            // Initialize the GUI components
            InitializeComponent();

            // Set focus to the first field
            this.txtName.Focus();
        }

        /// <summary>
        /// Re-enables the owning window after this one is closed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closed(object sender, EventArgs e)
        {
            this.Owner.IsEnabled = true;
        }

        /// <summary>
        /// Handles saving the new CustomFieldType.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            int i = 0;
            StringBuilder errorMessages = new StringBuilder();

            // Validate fields
            if (!(this.customFieldErrorProvider.ValidateFinal()))
            {
                // Put the error list into a string
                foreach (string nextError in this.customFieldErrorProvider.ErrorList)
                {
                    // Increment our counter
                    i++;

                    // Build the error list
                    errorMessages.Append("[" + i.ToString() + "] " + nextError + "\n");
                }
                MessageBox.Show(this, errorMessages.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            // Attempt to save
            if (!(this._customFieldBusiness.AddCustomFieldType(this._newCustomFieldType)))
            {
                MessageBox.Show(this, "An error was encountered while trying to add your new Custom Field Type.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            this.Close();
        }

        /// <summary>
        /// Cancels out of this window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
